package de.michaelhuebl.demo.helper;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.util.Log;
import de.michaelhuebl.demo.Location;

/**
 * Parses an given XML-String with W3C-DOM.
 * 
 * Searches for <title> and <BusinessClickUrl>-Tags.
 * Saves the content in an ArrayList<String>.
 * 
 * @author Michael Huebl
 *
 */
public class ParseXmlWithDom {
	
	ArrayList<Location> locations = new ArrayList<Location>();
	
	/**
	 * Parses an given XML. Saves it in the titleOfRestaurants
	 * ArrayList.
	 * <br>
	 * 
	 * Required XML-pattern:
	 * <Result>
	 *  <Title/>
	 *  <BusinessClickUrl/>
	 * </Result>
	 * 
	 * @param xml XML-code as String
	 */
	public void parseXml(String xml) {
		
		String sUrl = "";
		String sTitle = "";
		
		
		// Reset the ArrayList
		locations.clear();
		
		try {
			// Create required instances
			DocumentBuilderFactory dbf;
			dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			
			// Parse the xml
			Document doc = db.parse(new InputSource(new StringReader(xml)));
			
			// Get all <Result> tags.
			// If there are more than zero tags, we know, that we have a result. 
			NodeList nl = doc.getElementsByTagName("Result");			
			if (nl != null && nl.getLength() > 0) {
				
				// Get the title and url for every <Result>-tag 
				for (int i = 0; i < nl.getLength(); i++) {
					
					Location l = new Location();
					
					// Get the content of the <Result>-tag
					Element result = (Element)nl.item(i);
					
					// Get the content of the <Title>-tag inside the <Result>-tag
					Element title = (Element) result.getElementsByTagName("Title").item(0);
					sTitle = title.getFirstChild().getNodeValue();
					l.setTitle(sTitle);
					Log.v("Demo", "Content of parsed element: " + sTitle);
					
					// Get the content of the <BusinessClickUrl>-tag inside the <Result>-tag
					Element url = (Element) result.getElementsByTagName("BusinessClickUrl").item(0);
					if (url.hasChildNodes()) {
						sUrl = url.getFirstChild().getNodeValue();
						l.setBusinessClickUrl(sUrl);
						Log.v("Demo", "Content of parsed element: " + sUrl);
					}
					
					// Save the content in the ArrayList
					locations.add(l);
				}
			}
		} 
		catch (ParserConfigurationException e) {
			Log.v("Demo", "ParserConfigurationException " + e.toString());
		} catch (SAXException e) {
			Log.v("Demo", "SAXException " + e.toString());
		} catch (IOException e) {
			Log.v("Demo", "IOException " + e.toString());
		}
	}
	
	/**
	 * Returns the result of xml parsing.
	 * Call this after calling parseXML().
	 * 
	 * @return ArrayList
	 */
	public ArrayList<Location> getParseResult() {
		return locations;
	}
}
